﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPC.LogCommon.DelegateAndEvent
{
    public class DelegateAndEvent
    {
        #region 单例模式
        private static DelegateAndEvent? _instance;
        public static DelegateAndEvent? Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DelegateAndEvent();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }
        #endregion

        #region 值刷新时更新界面
        public delegate void RefreshFrmWhenValueUpdateDelegate(string value, string fullName, int updateCount);
        public event RefreshFrmWhenValueUpdateDelegate? RefreshFrmWhenValueUpdateEvent;

        /// <summary>
        /// 刷新界面值
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="fullName">全名</param>
        public void RefreshFrmWhenValueUpdate(string value, string fullName, int updateCount)
        {
            if(RefreshFrmWhenValueUpdateEvent != null)
            {
                RefreshFrmWhenValueUpdateEvent(value, fullName, updateCount);
            }
        }

        /// <summary>
        /// 清除刷新界面值代理
        /// </summary>
        public void ClearFrmWhenValueUpdate()
        {
            if (RefreshFrmWhenValueUpdateEvent == null) return;
            Delegate[] delegates = RefreshFrmWhenValueUpdateEvent.GetInvocationList();
            foreach(Delegate delegator in delegates)
            {
                RefreshFrmWhenValueUpdateEvent -= delegator as RefreshFrmWhenValueUpdateDelegate;
            }
        }
        #endregion

        #region 添加报错信息
        public delegate void AddAlarmInfoDelegate(string text);
        public event AddAlarmInfoDelegate? AddAlarmInfoEvent;

        /// <summary>
        /// 添加报错信息
        /// </summary>
        /// <param name="text"></param>
        public void AddAlarmInfo(string text)
        {
            if (AddAlarmInfoEvent != null)
            {
                AddAlarmInfoEvent(text);
            }
        }

        /// <summary>
        /// 清除添加报错信息代理
        /// </summary>
        public void ClearAddAlarmInfo()
        {
            if (AddAlarmInfoEvent == null) return;
            Delegate[] delegates = AddAlarmInfoEvent.GetInvocationList();
            foreach (Delegate delegator in delegates)
            {
                AddAlarmInfoEvent -= delegator as AddAlarmInfoDelegate;
            }
        }
        #endregion

        #region 刷新图表
        public delegate void RefreshSeriesDelegate (string fullName, string recordTime, string value);
        public event RefreshSeriesDelegate ? RefreshSeriesEvent;
        public void RefreshSeries(string fullName,string recordTime, string value)
        {
            if (RefreshSeriesEvent != null)
            {
                RefreshSeriesEvent(fullName, recordTime, value);
            }
        }
        #endregion
    }
}
